package com.hntx.joymusic.bus;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.subjects.PublishSubject;

/**
 * Created by Administrator on 2017/8/8.
 */

public class RxBus {
    static Map<String, RxBus> buses = new HashMap<String, RxBus>();
    private static RxBus rxBus;
    private PublishSubject<Object> bus;

    public RxBus() {
        bus = PublishSubject.create();
    }

    public static RxBus getInstance() {
        if (rxBus == null) {
            synchronized (RxBus.class) {
                if (rxBus == null) {
                    rxBus = new RxBus();
                }
            }
        }
        return rxBus;
    }

    public static RxBus getInstance(String name) {

        RxBus bus = buses.get(name);
        if (bus != null) {
            return bus;
        }
        synchronized (RxBus.class) {
            bus = new RxBus();
            buses.put(name, bus);
        }
        return bus;
    }

    public void send(Object o) {
        bus.onNext(o);
    }

    public void register() {
    }

    public void unregister() {
    }

    public Observable<Object> toObservable() {
        return bus;
    }

    public boolean hasObservers() {
        return bus.hasObservers();
    }
}
